package com.weimob.mcloud.wechatwork.customer.config;

import com.weimob.dq.DqClientFactory;
import com.weimob.dq.client.Settings;
import com.weimob.dq.client.api.DqFramework;
import com.weimob.dq.client.retry.RetryStrategy;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;

/**
 * 延迟队列配置
 *
 * @author jun.liu05
 * @since 1.0
 */
@Data
@Configuration
public class DqConfig {

    public final static String CUSTOMER_SEARCH_MSG_TOPIC= "wechatwork_push_customer_search_msg";

    public final static String CUSTOMER_LOSE_MSG = "wechatwork_customer_lose_msg";

    public final static String CUSTOMER_STAGE_REMIND = "wechatwork_customer_stage_remind";
    /**
     * 接收外部联系人消息后，可能还未生成wid，如果没有生成wid需要做补偿
     */
    @Value("${customer.search.msg.topic.token:}")
    private String wechatWorkSopPushHandlerTopicToken;

    @Value("${customer.lose.msg.topic.token:}")
    private String customerLoseMsgTopicToken;

    /**
     * QA :75326M648524321
     * OL :7847b6097058011
     */
    @Value("${customer.stage.remind.token:}")
    private String customerStageRemindToken;

    @Bean
    public DqFramework dqClient() {
        Settings settings = Settings.builder()
                // 设置topic的name和token信息。该信息需要到虫洞申请并且被审核通过。
                .authTopic(CUSTOMER_SEARCH_MSG_TOPIC, wechatWorkSopPushHandlerTopicToken)
                .authTopic(CUSTOMER_LOSE_MSG, customerLoseMsgTopicToken)
                .authTopic(CUSTOMER_STAGE_REMIND,customerStageRemindToken)
                // 为这个topic开启重试
                .enableRetry(
                        CUSTOMER_SEARCH_MSG_TOPIC,
                        CUSTOMER_LOSE_MSG,
                        CUSTOMER_STAGE_REMIND
                )
                // 配置全局的重试策略（重试次数+每次重试间隔）
                .configDefaultRetryStrategy(new RetryStrategy(1, new Duration[]{Duration.ofSeconds(180)}))
                .build();
        return DqClientFactory.newDqClient(settings);
    }
}
